﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MinoSoft.sslpDataSetTableAdapters;

namespace MinoSoft
{
    public partial class FrmAgregarCompra : Form
    {
        public FrmAgregarCompra()
        {
            InitializeComponent();
        }    

        private void FrmAgregarCompra_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void FrmAgregarCompra_Load(object sender, EventArgs e)
        {
            dgvAgregarCompra.Rows.Insert(0, 1);
            dgvAgregarCompra.Rows[0].Cells["FECHA"].Value = DateTime.Now;
        }

        private void dgvAgregarCompra_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Agrego una nueva fila si:
            //1) Completo todos los datos de la fila  y..
            //2) Esta parado en la ultima celda de la fila y..
            //3) Esta parado en la ultima fila y..
            //3) Presionó Enter
            if(dgvAgregarCompra.CurrentCell.ColumnIndex == 3)
            {
                if (dgvAgregarCompra.CurrentCell.RowIndex+1 == dgvAgregarCompra.RowCount
                    && e.KeyChar == Convert.ToChar(Keys.Enter)
                    && filaCompleta())
                {
                    dgvAgregarCompra.Rows.Add(DateTime.Now, "", "", "");
                    dgvAgregarCompra.Rows[dgvAgregarCompra.RowCount - 1].Cells[0].Selected = true;
                }
            }

        }

        
        private bool filaCompleta()
        {
            for(int i = 0; i < dgvAgregarCompra.Columns.Count;i++)
            {
                if (dgvAgregarCompra.CurrentRow.Cells[i].Value.ToString() == "")
                    return false;
            }
            return true;
        }

        private void dgvAgregarCompra_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvAgregarCompra.CurrentCell.ColumnIndex == 1)
            {
                TextBox tb = (TextBox)e.Control;
                if (tb != null)
                {
                    tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                    tb.AutoCompleteCustomSource = AutoCompleteIDArticulo();
                    tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
                }
            }
        }

        /// <summary>
        /// Metodo para autocompletar el ID de Articulo
        /// </summary>
        /// <returns></returns>
        public AutoCompleteStringCollection AutoCompleteIDArticulo()
        {
            AutoCompleteStringCollection str = new AutoCompleteStringCollection();
            ARTICULOSTableAdapter articulos = new ARTICULOSTableAdapter();
            sslpDataSet.ARTICULOSDataTable dt = articulos.GetData();

            foreach (DataRow row in dt.Rows)
                str.Add(row["IDARTICULO"].ToString());
            return str;
        }

        private void dgvAgregarCompra_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvAgregarCompra.CurrentCell.ColumnIndex == 1)
            {
                ARTICULOSTableAdapter articulos = new ARTICULOSTableAdapter();
                sslpDataSet.ARTICULOSDataTable dt = new sslpDataSet.ARTICULOSDataTable();

                articulos.FillByID(dt, dgvAgregarCompra.CurrentRow.Cells[1].Value.ToString());
                dgvAgregarCompra.CurrentRow.Cells["DESCRIPCION"].Value = dt[0].DESCRIPCION;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            STOCKTableAdapter stock = new STOCKTableAdapter();
            ARTICULOSTableAdapter articulo = new ARTICULOSTableAdapter();
            sslpDataSet.ARTICULOSDataTable dt = new sslpDataSet.ARTICULOSDataTable();

            try
            {
                foreach (DataGridViewRow row in dgvAgregarCompra.Rows)
                {
                    String IDart = row.Cells["ARTICULO"].Value.ToString();
                    int cantCompra = int.Parse(row.Cells["CANTIDAD"].Value.ToString());

                    //Se actualiza tabla STOCK
                    stock.Insert(Secuenciador.getInstance().nextIDStock(), DateTime.Now, IDart, 0, cantCompra);

                    //Se actualiza ficha articulo
                    articulo.FillByID(dt, IDart);
                    articulo.UpdateQueryByID(dt[0].STOCK + cantCompra, IDart);
                }
            }
            catch (Exception ex)
            {
                Dialogos.error(ex.Message);
            }
            finally
            {
                this.Close();
            }
        }       
    }
}
